package cn.lhl.service.system.impl;

import cn.lhl.dao.system.UserDao;
import cn.lhl.domain.system.User;
import cn.lhl.service.system.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    public PageInfo findAll(int page, int size, String companyId) {
        //调用PageHelper
        PageHelper.startPage(page, size);
        //下一语句将会执行分页
        List<User> userList = userDao.findAll(companyId);
        return new PageInfo(userList);
    }

    public User findById(String id) {
        User user = userDao.findById(id);
        return user;
    }

    public void save(User user) {
        userDao.save(user);
    }

    public void update(User user) {
        userDao.update(user);
    }

    public void delete(String id) {
        userDao.delete(id);
    }


    public void changeRole(String userId, String roleIds) {
        //1. 通过userId删除这个用户所有的角色信息
        userDao.deleteByUserId(userId);

        //2. 循环roleIds，将userid,roleid写入中间表
        String[] strings = roleIds.split(",");
        for (String roleId : strings) {
            userDao.insertUserRole(userId, roleId);
        }
    }

    //验证是否有该用户的信息有则返回
    public User findByEmail(String email) {
        User user = userDao.findByEmail(email);
        return user;
    }
}
